From a46819c02ba5b048e126f429f6320ff3030e404a Mon Sep 17 00:00:00 2001
From: q66 <q66@chimera-linux.org>
Date: Sat, 11 Mar 2023 22:53:42 +0100
Subject: [PATCH] sh(1): expand ulimit to match common Linux implementations

This includes stuff like nice and rt priority.
---
 patches/src.freebsd.patch  | 123 ++++++++++++++++++++++++++++++++++++-
 src.freebsd/sh/miscbltin.c |  29 ++++++++-
 src.freebsd/sh/sh.1        |  27 +++-----
 3 files changed, 158 insertions(+), 21 deletions(-)

diff --git a/patches/src.freebsd.patch b/patches/src.freebsd.patch
index 7214d7d..24e5fe6 100644
--- a/patches/src.freebsd.patch
+++ b/patches/src.freebsd.patch
@@ -8258,7 +8258,64 @@
  	char *stacknxt;
 --- src.orig/sh/miscbltin.c
 +++ src.freebsd/sh/miscbltin.c
-@@ -567,7 +567,8 @@
+@@ -444,6 +444,13 @@
+ 	char	option;
+ };
+ 
++/* linux */
++#ifndef RLIMIT_VMEM
++#ifdef RLIMIT_AS
++#define RLIMIT_VMEM RLIMIT_AS
++#endif
++#endif
++
+ static const struct limits limits[] = {
+ #ifdef RLIMIT_CPU
+ 	{ "cpu time",		"seconds",	RLIMIT_CPU,	   1, 't' },
+@@ -475,7 +482,8 @@
+ #ifdef RLIMIT_VMEM
+ 	{ "virtual mem size",	"kbytes",	RLIMIT_VMEM,	1024, 'v' },
+ #endif
+-#ifdef RLIMIT_SWAP
++#if 0
++#ifdef
+ 	{ "swap limit",		"kbytes",	RLIMIT_SWAP,	1024, 'w' },
+ #endif
+ #ifdef RLIMIT_SBSIZE
+@@ -490,6 +498,23 @@
+ #ifdef RLIMIT_UMTXP
+ 	{ "umtx shared locks",	(char *)0,	RLIMIT_UMTXP,	   1, 'o' },
+ #endif
++#else
++#ifdef RLIMIT_LOCKS
++	{ "locks",		(char *)0,	RLIMIT_LOCKS,	   1, 'w' },
++#endif
++#ifdef RLIMIT_SIGPENDING
++	{ "pending signals",		(char *)0,	RLIMIT_SIGPENDING,	   1, 'i' },
++#endif
++#ifdef RLIMIT_MSGQUEUE
++	{ "POSIX msg queue",	"bytes",	RLIMIT_MSGQUEUE,	   1, 'q' },
++#endif
++#ifdef RLIMIT_NICE
++	{ "max nice",		(char *)0,	RLIMIT_NICE,	   1, 'e' },
++#endif
++#ifdef RLIMIT_RTPRIO
++	{ "rt priority",		(char *)0,	RLIMIT_RTPRIO,	   1, 'r' },
++#endif
++#endif
+ 	{ (char *) 0,		(char *)0,	0,		   0, '\0' }
+ };
+ 
+@@ -525,7 +550,7 @@
+ 	struct rlimit	limit;
+ 
+ 	what = 'f';
+-	while ((optc = nextopt("HSatfdsmcnuvlbpwko")) != '\0')
++	while ((optc = nextopt("HSatfdsmcnuvlbpwkoreiq")) != '\0')
+ 		switch (optc) {
+ 		case 'H':
+ 			how = HARD;
+@@ -567,7 +592,8 @@
  				error("bad number");
  			uval *= l->factor;
  			val = (rlim_t)uval;
@@ -8656,6 +8713,70 @@
  .It Fl s Li stdin
  Read commands from standard input (set automatically
  if no file arguments are present).
+@@ -2630,7 +2614,7 @@
+ For aliases the alias expansion is printed;
+ for commands and tracked aliases
+ the complete pathname of the command is printed.
+-.It Ic ulimit Oo Fl HSabcdfklmnopstuvw Oc Op Ar limit
++.It Ic ulimit Oo Fl HSacedfilmnqrstuvw Oc Op Ar limit
+ Set or display resource limits (see
+ .Xr getrlimit 2 ) .
+ If
+@@ -2669,8 +2653,6 @@
+ displayed or modified.
+ They are mutually exclusive.
+ .Bl -tag -width indent
+-.It Fl b Ar sbsize
+-The maximum size of socket buffer usage, in bytes.
+ .It Fl c Ar coredumpsize
+ The maximal size of core dump files, in 512-byte blocks.
+ Setting
+@@ -2678,13 +2660,12 @@
+ to 0 prevents core dump files from being created.
+ .It Fl d Ar datasize
+ The maximal size of the data segment of a process, in kilobytes.
++.It Fl e Ar nice
++The nice value ceiling for the process.
+ .It Fl f Ar filesize
+ The maximal size of a file, in 512-byte blocks.
+-.It Fl k Ar kqueues
+-The maximal number of kqueues
+-(see
+-.Xr kqueue 2 )
+-for this user ID.
++.It Fl i Ar sigpending
++The limit on the number of signals that may be queued.
+ .It Fl l Ar lockedmem
+ The maximal size of memory that can be locked by a process, in
+ kilobytes.
+@@ -2692,13 +2673,10 @@
+ The maximal resident set size of a process, in kilobytes.
+ .It Fl n Ar nofiles
+ The maximal number of descriptors that could be opened by a process.
+-.It Fl o Ar umtxp
+-The maximal number of process-shared locks
+-(see
+-.Xr pthread 3 )
+-for this user ID.
+-.It Fl p Ar pseudoterminals
+-The maximal number of pseudo-terminals for this user ID.
++.It Fl r Ar rtprio
++The ceiling on the real-time priority that may be set for the process.
++.It Fl q Ar msgqueue
++The limit on the number of bytes that may be allocated for POSIX message queus.
+ .It Fl s Ar stacksize
+ The maximal size of the stack segment, in kilobytes.
+ .It Fl t Ar time
+@@ -2707,9 +2685,6 @@
+ The maximal number of simultaneous processes for this user ID.
+ .It Fl v Ar virtualmem
+ The maximal virtual size of a process, in kilobytes.
+-.It Fl w Ar swapuse
+-The maximum amount of swap space reserved or used for this user ID,
+-in kilobytes.
+ .El
+ .It Ic umask Oo Fl S Oc Op Ar mask
+ Set the file creation mask (see
 --- src.orig/sh/trap.c
 +++ src.freebsd/sh/trap.c
 @@ -58,8 +58,9 @@
diff --git a/src.freebsd/sh/miscbltin.c b/src.freebsd/sh/miscbltin.c
index 1f88e88..691a0cb 100644
--- a/src.freebsd/sh/miscbltin.c
+++ b/src.freebsd/sh/miscbltin.c
@@ -444,6 +444,13 @@ struct limits {
 	char	option;
 };
 
+/* linux */
+#ifndef RLIMIT_VMEM
+#ifdef RLIMIT_AS
+#define RLIMIT_VMEM RLIMIT_AS
+#endif
+#endif
+
 static const struct limits limits[] = {
 #ifdef RLIMIT_CPU
 	{ "cpu time",		"seconds",	RLIMIT_CPU,	   1, 't' },
@@ -475,7 +482,8 @@ static const struct limits limits[] = {
 #ifdef RLIMIT_VMEM
 	{ "virtual mem size",	"kbytes",	RLIMIT_VMEM,	1024, 'v' },
 #endif
-#ifdef RLIMIT_SWAP
+#if 0
+#ifdef
 	{ "swap limit",		"kbytes",	RLIMIT_SWAP,	1024, 'w' },
 #endif
 #ifdef RLIMIT_SBSIZE
@@ -489,6 +497,23 @@ static const struct limits limits[] = {
 #endif
 #ifdef RLIMIT_UMTXP
 	{ "umtx shared locks",	(char *)0,	RLIMIT_UMTXP,	   1, 'o' },
+#endif
+#else
+#ifdef RLIMIT_LOCKS
+	{ "locks",		(char *)0,	RLIMIT_LOCKS,	   1, 'w' },
+#endif
+#ifdef RLIMIT_SIGPENDING
+	{ "pending signals",		(char *)0,	RLIMIT_SIGPENDING,	   1, 'i' },
+#endif
+#ifdef RLIMIT_MSGQUEUE
+	{ "POSIX msg queue",	"bytes",	RLIMIT_MSGQUEUE,	   1, 'q' },
+#endif
+#ifdef RLIMIT_NICE
+	{ "max nice",		(char *)0,	RLIMIT_NICE,	   1, 'e' },
+#endif
+#ifdef RLIMIT_RTPRIO
+	{ "rt priority",		(char *)0,	RLIMIT_RTPRIO,	   1, 'r' },
+#endif
 #endif
 	{ (char *) 0,		(char *)0,	0,		   0, '\0' }
 };
@@ -525,7 +550,7 @@ ulimitcmd(int argc __unused, char **argv __unused)
 	struct rlimit	limit;
 
 	what = 'f';
-	while ((optc = nextopt("HSatfdsmcnuvlbpwko")) != '\0')
+	while ((optc = nextopt("HSatfdsmcnuvlbpwkoreiq")) != '\0')
 		switch (optc) {
 		case 'H':
 			how = HARD;
diff --git a/src.freebsd/sh/sh.1 b/src.freebsd/sh/sh.1
index 06f5a0f..844cbfe 100644
--- a/src.freebsd/sh/sh.1
+++ b/src.freebsd/sh/sh.1
@@ -2614,7 +2614,7 @@ and not found.
 For aliases the alias expansion is printed;
 for commands and tracked aliases
 the complete pathname of the command is printed.
-.It Ic ulimit Oo Fl HSabcdfklmnopstuvw Oc Op Ar limit
+.It Ic ulimit Oo Fl HSacedfilmnqrstuvw Oc Op Ar limit
 Set or display resource limits (see
 .Xr getrlimit 2 ) .
 If
@@ -2653,8 +2653,6 @@ The remaining options specify which resource value is to be
 displayed or modified.
 They are mutually exclusive.
 .Bl -tag -width indent
-.It Fl b Ar sbsize
-The maximum size of socket buffer usage, in bytes.
 .It Fl c Ar coredumpsize
 The maximal size of core dump files, in 512-byte blocks.
 Setting
@@ -2662,13 +2660,12 @@ Setting
 to 0 prevents core dump files from being created.
 .It Fl d Ar datasize
 The maximal size of the data segment of a process, in kilobytes.
+.It Fl e Ar nice
+The nice value ceiling for the process.
 .It Fl f Ar filesize
 The maximal size of a file, in 512-byte blocks.
-.It Fl k Ar kqueues
-The maximal number of kqueues
-(see
-.Xr kqueue 2 )
-for this user ID.
+.It Fl i Ar sigpending
+The limit on the number of signals that may be queued.
 .It Fl l Ar lockedmem
 The maximal size of memory that can be locked by a process, in
 kilobytes.
@@ -2676,13 +2673,10 @@ kilobytes.
 The maximal resident set size of a process, in kilobytes.
 .It Fl n Ar nofiles
 The maximal number of descriptors that could be opened by a process.
-.It Fl o Ar umtxp
-The maximal number of process-shared locks
-(see
-.Xr pthread 3 )
-for this user ID.
-.It Fl p Ar pseudoterminals
-The maximal number of pseudo-terminals for this user ID.
+.It Fl r Ar rtprio
+The ceiling on the real-time priority that may be set for the process.
+.It Fl q Ar msgqueue
+The limit on the number of bytes that may be allocated for POSIX message queus.
 .It Fl s Ar stacksize
 The maximal size of the stack segment, in kilobytes.
 .It Fl t Ar time
@@ -2691,9 +2685,6 @@ The maximal amount of CPU time to be used by each process, in seconds.
 The maximal number of simultaneous processes for this user ID.
 .It Fl v Ar virtualmem
 The maximal virtual size of a process, in kilobytes.
-.It Fl w Ar swapuse
-The maximum amount of swap space reserved or used for this user ID,
-in kilobytes.
 .El
 .It Ic umask Oo Fl S Oc Op Ar mask
 Set the file creation mask (see
